<?php
class ControllerDelimanBaocaokhachhangdatbaotheoky extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/kybao");
	 	$this->load->model("deliman/khachhang");
	 	$this->load->model("deliman/nhanvien");
		$this->load->model('deliman/khuvuc');
		$this->load->model("common/control");
   	}
	
	public function index()
	{
		$this->getList();
	}
		
	private function getList() 
	{		
		$data = $this->request->get;		
		$this->data['dskhachhangdatbao']=array();
		
		// Danh sach LOAI BAO co khach hang da dat		
		$where = "";	
		
		$this->data['dsloaibao']=array();// COMBOBOX LOAI BAO
		$this->data['dsloaibao'] = $this->model_deliman_loaibao->getList($where);
		
		$where = "";
		if(trim($data['tungay'])!='')
		{
			$where .= " AND ngayphathanh >= '".date('Y-m-d',strtotime($data['tungay']))."'";
		}
		if(trim($data['denngay'])!='')
		{
			$where .= " AND ngayphathanh <= '".date('Y-m-d',strtotime($data['denngay']))." 24:00:00'";
		}
		if(trim($data['loaibaoid'])!='')
		{
			$where .= " AND loaibaoid = '".$data['loaibaoid']."'";
		}
		if(trim($data['kybaoid'])!='')
		{			
			//Danh sach ky bao ma khach hang da dat
			$where .= " AND kybaoid = '".$data['kybaoid']."'";			
		}
		if(trim($where)=="")
		{
			$where .= " AND 1=0";// Lan dau goi len ko load ds
		}
		
		// Lay ds khuvucid của các khu vực mà nhân viên đang đăng nhập quản lý
		$dskhuvucphutrachid = array();
		$dskh=array();
		
		if(trim($this->user->getNhanVien('loainhanvienid')) == '2')
		{
			// Lay nhung KHU VUC ma nhan vien QL
			$dskhuvucphutrachid = $this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid ='".$this->user->getNhanVien('nhanvienid')."' AND tinhtrang != 'deleted' ORDER BY khuvucid ASC");
			$dskhuvucphutrachid = $this->string->matrixToArray($dskhuvucphutrachid, 'khuvucid');
		}
		else
		{
			$dskhuvucphutrachid[]='0';
		}
		
		$dskv = array();
		$khuvus=array();
		if(count($dskhuvucphutrachid)>0)
		{
			foreach($dskhuvucphutrachid as $kvid)
			{
				$this->model_deliman_khuvuc->getTree($kvid, $khuvus);
				$dskv = array_merge($dskv, $khuvus);
			}
		}
		$dskv = $this->string->matrixToArray($dskv,'khuvucid');
		$dskv = array_unique($dskv);
		//Lay ds nhung KHACH HANG trong khu vuc do
		$dskhid = $this->model_deliman_khachhang->getList(" AND khuvucid in( '".implode("','",$dskv)."')");
		$dskhid = $this->string->matrixToArray($dskhid,'khachhangid');
		// Tao dk: chi lay thongtindatbao cua nhung KHACH HANG nam trong khu vuc QL
		$where .= " AND khachhangid in( '".implode("','",$dskhid)."')";
		
		$where .= " ORDER BY ngayphathanh DESC";
		$dskhachhangdatbao=array();
		$dskhachhangdatbao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);	
		// Chi lay cac FIELD can thiet: DU LIEU TREN TABLE
		for($i=0; $i < count($dskhachhangdatbao); $i++)
		{
			$khachhang = $this->model_deliman_khachhang->getItem($dskhachhangdatbao[$i]['khachhangid']);	
			$this->data['dskhachhangdatbao'][$i]['tenkhachhang'] = $khachhang['hoten'];
			$this->data['dskhachhangdatbao'][$i]['dienthoai'] = $khachhang['dienthoai'];
			$this->data['dskhachhangdatbao'][$i]['email'] = $khachhang['email'];
			$this->data['dskhachhangdatbao'][$i]['diachi'] = $khachhang['diachi'];
			$this->data['dskhachhangdatbao'][$i]['ngaydat'] = $dskhachhangdatbao[$i]['ngaydat'];
			$this->data['dskhachhangdatbao'][$i]['soluongdat'] = $dskhachhangdatbao[$i]['soluongdat'];
			
			// Tuy chon them de xu ly
			$this->data['dskhachhangdatbao'][$i]['khuvucid'] = $khachhang['khuvucid'];
			$this->data['dskhachhangdatbao'][$i]['loaibaoid'] = $dskhachhangdatbao[$i]['loaibaoid'];
			$this->data['dskhachhangdatbao'][$i]['kybaoid'] = $dskhachhangdatbao[$i]['kybaoid'];
		}
		
		$this->data['dskybao']=array();
		$dskybaoid = array();
		//= nhung KY BAO co khach hang dat bao: => Dung de gom nhom theo KY BAO
		$dskybaoid = $this->string->matrixToArray($this->data['dskhachhangdatbao'],'kybaoid');
		$dskybaoid = array_unique($dskybaoid);
		// Lay thong tin CHI TIET cua loai bao
		foreach($dskybaoid as $kybaoid)
		{
			$kybao = $this->model_deliman_kybao->getItem($kybaoid);
			array_push($this->data['dskybao'], $kybao);
		}
		// Muốn sort thi ..
			
		$this->data['dskhuvuc']=array();
		$dskhuvucid = array();
		//= nhung KHU VUC co khach hang dat bao: => => Dung de gom nhom theo KHU VUC
		$dskhuvucid = $this->string->matrixToArray($this->data['dskhachhangdatbao'],'khuvucid');
		$dskhuvucid = array_unique($dskhuvucid);
		// Lay thong tin CHI TIET cua KHU VUC
		foreach($dskhuvucid as $khuvucid)
		{
			$khuvuc = $this->model_deliman_khuvuc->getItem($khuvucid);
			array_push($this->data['dskhuvuc'], $khuvuc);
		}
		
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/baocaokhachhangdatbaotheoky.tpl";
		if($data['print'] == "true")
			$this->layout="layout/print";
		else
			$this->layout="layout/deliman";
		
		$this->render();
	}
	
	// Post ds ky bao theo loai bao da chon vao combobox
	public function getKyBao()
	{
		$where = "";
		$loaibaoid =  $this->request->get['loaibaoid'];
		$tungay =  $this->request->get['tungay'];
		$denngay =  $this->request->get['denngay'];
		$select =  $this->request->get['select'];
		$where .= " AND loaibaoid = '".$loaibaoid."'";
		if(trim($tungay)!='')
		{
			$where .= " AND ngayphathanh >= '".date('Y-m-d',strtotime($tungay))."'";
		}
		if(trim($denngay)!='')
		{	
			$where .= " AND ngayphathanh <= '".date('Y-m-d',strtotime($denngay))."'";
		}
		$cmbkybao = $this->model_deliman_kybao->getList($where." ORDER BY ngayphathanh DESC");
		$cb= $this->model_common_control->getDataCombobox($cmbkybao, "tenkybao", "kybaoid", $select);
		$this->data['output'] = '<option value="">Chọn tất cả</option>'.$cb;
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
}
?>